/// <reference path="../lib/lib.ts" />
var Unit = (function () {
    function Unit(x, y) {
        this._speed = 100;
        this._direction = Unit.FORWARD;
        var bodyDef = new b2BodyDef();
        var shape = new b2PolygonShape();
        var fixDef = new b2FixtureDef();
        bodyDef.type = b2Body.b2_dynamicBody;
        bodyDef.position.x = x;
        bodyDef.position.y = y;
        this._skin = new createjs.Bitmap("js/img/PlateBlock.jpg");
        this._skin.x = 1;
        this._skin.y = 1;
        this._skin.regX = 12;
        this._skin.regY = 12;
        this._skin.snapToPixel = true;
        this._skin.mouseEnabled = false;
        this._body = Game.getInstance().createDynamic(bodyDef, this._skin);
        shape.SetAsBox(20, 25);
        fixDef.shape = shape;
        fixDef.friction = 1;
        fixDef.restitution = 0;
        fixDef.filter.categoryBits = Category.UNIT;
        fixDef.filter.maskBits = Category.BLOCK + Category.WATER + Category.UNIT;
        this._body.CreateFixture(fixDef);
        this._body.SetSleepingAllowed(false);
        this._body.SetUserData(this);
        this._skin.x = x;
        this._skin.y = y;
    }
    Unit.prototype.update = function () {
        this._skin.x = this._body.GetWorldCenter().x;
        this._skin.y = this._body.GetWorldCenter().y;
    };
    Unit.prototype.Move = function (dir) {
        if (this._direction === dir) {
            var v = this.countVelocityVec(dir);
            this._body.SetLinearVelocity(v);
        }
        var x = this._body.GetPosition().x;
        var y = this._body.GetPosition().y;
        console.log(this.countVelocityVec(dir));
        this._body.SetLinearVelocity(this.countVelocityVec(dir));
        this._body.SetAngle(toRad(dir * 90));
        this._body.SetPosition(this.fixPos(dir));
    };
    Unit.prototype.Stop = function () {
        this._body.SetLinearVelocity(new b2Vec2(0, 0));
    };
    Unit.prototype.countVelocityVec = function (dir) {
        switch (dir) {
            case Unit.FORWARD:
                return new b2Vec2(0, -this._speed);
            case Unit.RIGHTWARD:
                return new b2Vec2(this._speed, 0);
            case Unit.BACKWARD:
                return new b2Vec2(0, this._speed);
            case Unit.LEFTWARD:
                return new b2Vec2(-this._speed, 0);
        }
    };
    Unit.prototype.fixPos = function (dir) {
        var x = this._body.GetPosition().x;
        var y = this._body.GetPosition().y;
        if (dir === Unit.FORWARD || dir === Unit.BACKWARD) {
            var xd = x % 24;
            if (xd >= (24 / 2))
                x = x + (24 - xd);
            else
                x = x - xd;
            return new b2Vec2(x, y);
        }
        else if (dir === Unit.RIGHTWARD || dir === Unit.LEFTWARD) {
            var yd = y % 24;
            if (yd >= (24 / 2))
                y = y + (24 - yd);
            else
                y = y - yd;
            return new b2Vec2(x, y);
        }
        return new b2Vec2(x, y);
    };
    Unit.FORWARD = 0;
    Unit.RIGHTWARD = 1;
    Unit.BACKWARD = 2;
    Unit.LEFTWARD = 3;
    return Unit;
})();
